home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: artemis.sto.fdata.se!news
- From: Niklas Mellin <niklas.mellin@sto.fdata.se>
- Subject: Re: casting a void pointer back to a function pointer
- Sender: news@artemis.sto.fdata.se (UseNet NetNews)
- Message-ID: <3163BDF7.351C@sto.fdata.se>
- Date: Thu, 4 Apr 1996 12:17:59 GMT
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=us-ascii
- References: <DLABELL.96Apr4021045@columbia.pcs.cnu.edu>
- Mime-Version: 1.0
- X-Mailer: Mozilla 2.0 (WinNT; I)
- Organization: WM-data F÷rsvarsdata AB, Sweden
-
- Daniel LaBell wrote:
- >
- > I'm going to skip explaining why I want to do this, and get right to the
- > problem. How do I cast a pointer to a function pointer?
- >
- > Here is trivial example that shows the problem.
- >
- > void foo1(){ cout << " foo1 " << endl; }
- > void foo2(){ cout << " foo2 " << endl; }
- > void foo3( int x )
- > { cout << " foo3, argument = " << x << endl; }
- >
- > void do_a_foo( void (*fun)() )
- > { (*fun)(); }
- > void do_a_foo2( int x, void (*fun) (int ) )
- > { (*fun) ( x ); }
- > int main()
- > {
- > do_a_foo ( foo1 );
- > do_a_foo ( foo2 );
- > do_a_foo2 ( 2, foo3 );
- > void * x=foo2;
- > do_a_foo (x); // I don't know the syntax to do this cast. :(
- > return 0;
- > }
- > I get this warning message from g++:
- > :/home/student/dlabell/C/test.cc: In function `int main()':
- > :/home/student/dlabell/C/test.cc:21: warning: ANSI C++ forbids implicit conversion from `void *' in argument passing
- > :
- > :Compilation finished at Thu Apr 4 01:18:20
- >
- > So, now my program isn't portable, and if I overload do_a_foo instead of
- > making do_a_foo2, it wouldn't compile at all. So, I need to know the syntax
- > for casting a pointer to a pointer to a function. Can this be done in a
- > portable way? ( how about for ANSI C? ) If not, can this be done in
- > g++/gcc? And also if it isnt ANSI wouldn't that violate the premise that
- > "any pointer can be cast to void * and back again without loss of
- > information"? Or can I declare a type of pointer to a function?
- >
- >
- > --
- > Daniel LaBell
-
- do_a_foo(x); // Daniel doesn't know the syntax to do this cast.
-
- Examples of correct syntax:
-
- do_a_foo(reinterpret_cast<void (*)()>(x)); // #1
- do_a_foo((void (*)())x); // #2
-
- typedef void (*FPtr)();
- do_a_foo(reinterpret_cast<FPtr>(x)); // #3
- do_a_foo((FPtr)x); // #4
-
- All 4 forms work on Borland 4.52 and I think all forms are supported by
- the draft standard and portable (at least on systems where function pointers
- are of the same size as datapointers).
-
- Pick the form you prefer, personally I would choose number 3, because
- I like the new casting syntax in C++, and I always use typedefs for
- function pointers. In C I would try number 4, and I expect it would
- work, but recently I've been doing a lot of C++, and very little
- plain C, so I don't know what ANSI C says about casting function pointers.
- For a better answer on the ANSI C question you should ask in comp.lang.c
-
- ---
- Niklas Mellin
-